package com.the9tcat.deliverycoming.modle;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.the9tcat.deliverycoming.DeliveryDetailActivity;
import com.the9tcat.deliverycoming.R;
import com.the9tcat.deliverycoming.data.Configure;
import com.the9tcat.deliverycoming.util.Logger;

/**
 * The Class NotificationCenter.
 * 
 * </br></br>For example:</br> NotificationCenter nc =
 * NotificationCenter.getNotificationCenter(this);</br>
 * nc.showNotification("Hello", "Your cloth has come to Chengdu",
 * DeliveryComing.class);
 * 
 * @author jpwl88@gmail.com
 * @copyright 9tcat 夜猫 www.9tcat.com All rights reserved.
 */
public class NotificationCenter {

	/** The m notificaton center. */
	private static NotificationCenter mNotificatonCenter;

	/** The m context. */
	private Context mContext;

	/** The m manager. */
	private NotificationManager mManager;

	private Configure mConfigure;

	// /** Notification 的唯一标识ID，在0~999循环 */
	// private int mNotifyId = 0;

	/**
	 * Instantiates a new notification center.
	 * 
	 * @param con
	 *            the Context
	 */
	private NotificationCenter(Context con) {
		mContext = con;
		mManager = (NotificationManager) mContext
				.getSystemService(Context.NOTIFICATION_SERVICE);
		mConfigure = Configure.getInstance(con);
	}

	/**
	 * Gets the NotificationCenter.
	 * 
	 * @param con
	 *            the context
	 * @return the notification center
	 */
	public static synchronized NotificationCenter getNotificationCenter(
			Context con) {
		if (mNotificatonCenter == null) {
			mNotificatonCenter = new NotificationCenter(con);
		}
		return mNotificatonCenter;
	}

	/**
	 * Show notification based on our setting.
	 * 
	 * @param title
	 *            The title
	 * @param content
	 *            The content
	 * @param targetActivity
	 *            The target activity to open
	 */
	public void showNotification(String title, String content, int trackId) {
		long currTimes = System.currentTimeMillis();
		if (mConfigure.isInDndTime()) {
			return;
		}

		Notification mNotification = new Notification();
		mNotification.when = currTimes;
		mNotification.flags = Notification.FLAG_AUTO_CANCEL;
		mNotification.icon = R.drawable.icon;

		// 设置提醒方式
		// 首先禁用系统级提示，否则不能自定义铃声
		mNotification.defaults &= ~Notification.DEFAULT_SOUND;
		if (mConfigure.isLedEnabled()) {
			mNotification.defaults |= Notification.DEFAULT_LIGHTS;
		} else {
			mNotification.defaults &= ~Notification.DEFAULT_LIGHTS;
		}
		if (mConfigure.isVebrateEnabled()) {
			mNotification.defaults |= Notification.DEFAULT_VIBRATE;
		} else {
			mNotification.defaults &= ~Notification.DEFAULT_VIBRATE;
		}
		if (mConfigure.isSoundEnabled()) {
			mNotification.sound = mConfigure.getSoundUri();
		}
		Intent intent = new Intent(mContext, DeliveryDetailActivity.class);
		intent.putExtra("trackId", trackId);
		intent.putExtra("ACTIVITY_NAME_KEY", "notification");
//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(String.valueOf(System.currentTimeMillis()));
		
//		看当前Top Activity是不是我们的主界面
//		ActivityManager manager = (ActivityManager) mContext
//				.getSystemService(Context.ACTIVITY_SERVICE);
//		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
//
//		if (null != runningTaskInfos) {
//			if (runningTaskInfos.get(0).topActivity.toString().contains("DeliveryComingActivity")) {
//				Logger.e("***", "是从主界面进来的");
//			}
//		}

		PendingIntent pt = PendingIntent.getActivity(mContext, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		mNotification.setLatestEventInfo(mContext, title, content, pt);
		mManager.notify(trackId, mNotification);
	}

	/**
	 * Cancel notification.
	 */
	public void removeAllNotification() {
		mManager.cancelAll();
	}

	public void removeNotification(int id) {
		Logger.d("***", "cancel " + id);
		mManager.cancel(id);
	}
}
